﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmEditClienti : Form
    {
        private string sCif;
        DataSet dsClienti = new DataSet();
        DataTable dtClienti = new DataTable();
        SqlDataAdapter daClienti = new SqlDataAdapter();
        string sClienti = string.Empty;
        SqlCommand commClienti = new SqlCommand();
        public static SqlConnection connStatic;
        SqlConnection conn;
        private static Form _singletonInstance = null;
        private Form formCalling;
     
        public frmEditClienti(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;

        }

        public void SetKeyValues(string sCif, Form formCalling)
        {
            this.sCif = sCif;
            this.formCalling = formCalling;
        }

        private void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static Form GetSingletonInstance(SqlConnection conection)
        {
            if (_singletonInstance == null || _singletonInstance.IsDisposed)
            {
                connStatic = conection;
                _singletonInstance = new frmEditClienti(connStatic);
            }
            return _singletonInstance;
        }

        private void frmEditClienti_Load(object sender, EventArgs e)
        {
            if (formCalling.Name.Equals("frmClienti"))
            {
            sClienti = "SELECT * FROM clienti WHERE cl_cif = '" + sCif + "'";

            daClienti = new SqlDataAdapter(sClienti, conn);
            dsClienti = new DataSet();

            daClienti.Fill(dsClienti, "clienti");

            dtClienti = dsClienti.Tables[0];

            txtDenumire.Text = dtClienti.Rows[0]["cl_denumire"].ToString();
            txtCif.Text = sCif;
            txtCod.Text = dtClienti.Rows[0]["cl_cod"].ToString();
            txtAdresa.Text = dtClienti.Rows[0]["cl_adresa"].ToString();
            txtBanca.Text = dtClienti.Rows[0]["cl_banca"].ToString();
            txtIban.Text = dtClienti.Rows[0]["cl_iban"].ToString();
            txtEmail.Text = dtClienti.Rows[0]["cl_email"].ToString();
            txtRegCom.Text = dtClienti.Rows[0]["cl_regc"].ToString();
            }
            else if (formCalling.Name.Equals("frmFactura"))
            {
                txtCif.Enabled = true;
                btnSterge.Visible = false;
                lblInfo.Text = "Ati ales sa adaugati datele urmatorului client:";
                txtDenumire.Text = txtCod.Text = txtCif.Text = txtRegCom.Text = txtBanca.Text = txtEmail.Text = txtAdresa.Text = txtIban.Text = string.Empty;
            
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati salvarea clientului ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
            try
            {
                string sMsg = string.Empty;

                if (txtDenumire.Text.Trim().Length == 0)
                {
                    sMsg = " Denumire ";
                    goto msgErr;
                }

                if (txtCif.Text.Trim().Length == 0)
                {
                    sMsg = " CIF ";
                    goto msgErr;
                }
                if (txtRegCom.Text.Trim().Length == 0)
                {
                    sMsg = " Registrul comertului ";
                    goto msgErr;
                }
                if (txtAdresa.Text.Trim().Length == 0)
                {
                    sMsg = " Adresa ";
                    goto msgErr;
                }


                CustomProcedures.conn = connStatic;
                if (formCalling.Name.Equals("frmFactura"))
                {
                    CustomProcedures.INSERTCLIENTI (txtCif.Text,
                                                     txtRegCom.Text,
                                                     txtDenumire.Text,
                                                     txtAdresa.Text,
                                                     txtCod.Text,
                                                     txtIban.Text,
                                                     txtBanca.Text,
                                                     txtEmail.Text,
                                                     System.DateTime.Today
                                                     );
                    MessageBox.Show("Salvarea clientului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    ((frmFactura)formCalling).CallBack(txtDenumire.Text);

                }
                if (formCalling.Name.Equals("frmClienti"))
                {
                    CustomProcedures.UPDATECLIENTI(txtRegCom.Text,
                                                     txtDenumire.Text,
                                                     txtAdresa.Text,
                                                     txtCod.Text,
                                                     txtIban.Text,
                                                     txtBanca.Text,
                                                     txtEmail.Text,
                                                     txtCif.Text
                                                     );
                    MessageBox.Show("Salvarea clientului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    ((frmClienti)formCalling).CallBack();
                }
                    this.Close();
                    return;
            msgErr: MessageBox.Show("Pentru adaugarea unui client, campul" + sMsg + "trebuie completat!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("CIF client deja definit!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnSterge_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati stergerea clientului?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            { 
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
           
            try
            {
                sClienti = "DELETE FROM clienti WHERE cl_cif = '" + sCif +"'";

                commClienti = new SqlCommand(sClienti, conn);


                SqlDataReader readerCl = commClienti.ExecuteReader();

                readerCl.Dispose();
                readerCl.Close();
                txtDenumire.Text = txtCod.Text = txtCif.Text = txtRegCom.Text = txtBanca.Text = txtEmail.Text = txtAdresa.Text = txtIban.Text = string.Empty;

                MessageBox.Show("Stergerea clientului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((frmClienti)formCalling).CallBack();
                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Stergerea clientului nu s-a efectuat. S-au intampinat erori la stergere!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
